package com.xayy.health.mgt.controller;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.result.ResultBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xayy.health.mgt.model.TelemetryData;
import com.xayy.health.mgt.model.dto.DataFeatureDto;
import com.xayy.health.mgt.model.vo.DataSourceVo;
import com.xayy.health.mgt.model.vo.tupu.EquipmentResponseVo;
import com.xayy.health.mgt.model.vo.tupu.NodeVo;
import com.xayy.health.mgt.model.vo.tupu.PropertieVo;
import com.xayy.health.mgt.model.vo.tupu.RelationshipVo;
import com.xayy.health.mgt.service.DataQualityService;
import com.xayy.health.mgt.service.SatelliteNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/5/9 20:48
 */
@RestController
@RequestMapping(value = "/test")
@Api(tags = "测试接口")
@SuppressWarnings(value = "all")
public class ApiController {

    @Autowired
    private SatelliteNodeService satelliteNodeService;

    @Autowired
    private DataQualityService dataQualityService;


    /**
     * 获取遥测数据
     *
     * @param telemetryDto 遥测入参
     * @return ResultBody
     */
    @ApiOperation(value = "获取遥测数据")
    @PostMapping("getTelemetry")
    public ResultBody train(@RequestBody DataFeatureDto telemetryDto) {
        List<TelemetryData> ycList = satelliteNodeService.getYcList(telemetryDto);
        return ResultBody.success(ycList);
    }


    /**
     * 质量分析
     * @param dataSourceVo sql入参
     * @return ResultBody
     */
    @ApiOperation(value = "质量分析")
    @PostMapping("Quality")
    public ResultBody excuteRunSql(@RequestBody DataSourceVo dataSourceVo) {
        CheckResult<Object> result = dataQualityService.excuteRunSql(dataSourceVo);
        return ResultBody.success(result);
    }


    /**
     * 测试接口
     * @param o 测试入参
     * @return ResultBody
     */
    @ApiOperation(value = "测试接口")
    @PostMapping("/test")
    public ResultBody getTaskTrendDataList(@RequestBody Object o) {
        JSONObject result = new JSONObject();
        JSONArray list = new JSONArray();
        for (int i = 0; i < 100; i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("time", "2022-01-01 00:00:00");
            jsonObject.put("ZK38", "10");
            list.add(jsonObject);
        }
        result.put("total", 100);
        result.put("list", list);
        return ResultBody.success(result);
    }


    /**
     * 星到分系统(第三方接口)
     * @return ResultBody
     */
    @ApiOperation(value = "星到分系统(第三方接口)")
    @GetMapping("/selectSystem")
    public ResultBody selectSystem() {
        EquipmentResponseVo equipmentResponseVo = new EquipmentResponseVo();
        List<NodeVo> nodes = new ArrayList<>();

        NodeVo nodeVo1 = new NodeVo();
        nodeVo1.setId("1");
        nodeVo1.setLabel("星");
        PropertieVo propertieVo1 = new PropertieVo();
        propertieVo1.setId("1");
        propertieVo1.setName("0a星");
        nodeVo1.setProperties(propertieVo1);
        nodes.add(nodeVo1);

        int x = 1;
        for (int i = 2; i < 7; i++) {
            NodeVo fxtVo = NodeVo.builder().id(String.valueOf(i)).label("分系统").build();
            PropertieVo propertieVo = PropertieVo.builder().id(String.valueOf(i)).name("分系统" + x++).build();
            fxtVo.setProperties(propertieVo);
            nodes.add(fxtVo);
        }

        equipmentResponseVo.setNodes(nodes);
        List<RelationshipVo> relationships = new ArrayList<>();

        RelationshipVo relationshipVo1 = new RelationshipVo();
        relationshipVo1.setId("1");
        relationshipVo1.setStart("1");
        relationshipVo1.setEnd("2");
        relationshipVo1.setLabel("星_分系统");
        relationships.add(relationshipVo1);

        RelationshipVo relationshipVo2 = new RelationshipVo();
        relationshipVo2.setId("2");
        relationshipVo2.setStart("1");
        relationshipVo2.setEnd("3");
        relationshipVo2.setLabel("星_分系统");
        relationships.add(relationshipVo2);

        equipmentResponseVo.setRelationships(relationships);

        return ResultBody.success(equipmentResponseVo);
    }


    /**
     * 分系统到部件(第三方接口)
     * @param httpServletRequest
     * @return ResultBody
     */
    @ApiOperation(value = "分系统到部件(第三方接口)")
    @GetMapping("/selectComponent")
    public ResultBody selectComponent(HttpServletRequest httpServletRequest) {
        if (!"2".equals(httpServletRequest.getParameter("id"))) {
            return ResultBody.success();
        }
        EquipmentResponseVo equipmentResponseVo = new EquipmentResponseVo();
        List<NodeVo> nodes = new ArrayList<>();

        int id = 11;
        for (int i = 1; i < 300; i++) {
            NodeVo nodeVo = NodeVo.builder().id(String.valueOf(id++)).label("设备").build();
            PropertieVo propertieVo = PropertieVo.builder().id(String.valueOf(id)).name("设备" + i).build();
            nodeVo.setProperties(propertieVo);
            nodes.add(nodeVo);
        }
        NodeVo nodeVo4 = NodeVo.builder().id("13").label("设备").build();
        equipmentResponseVo.setNodes(nodes);
        List<RelationshipVo> relationships = new ArrayList<>();

        RelationshipVo relationshipVo1 = new RelationshipVo();
        relationshipVo1.setId("1");
        relationshipVo1.setStart("2");
        relationshipVo1.setEnd("11");
        relationshipVo1.setLabel("分系统_设备");
        relationships.add(relationshipVo1);

        RelationshipVo relationshipVo2 = new RelationshipVo();
        relationshipVo2.setId("2");
        relationshipVo2.setStart("2");
        relationshipVo2.setEnd("12");
        relationshipVo2.setLabel("分系统_设备");
        relationships.add(relationshipVo2);

        equipmentResponseVo.setRelationships(relationships);

        return ResultBody.success(equipmentResponseVo);
    }


    /**
     * 部件到参数(第三方接口)
     * @param httpServletRequest request请求
     * @return ResultBody
     */
    @ApiOperation(value = "部件到参数(第三方接口)")
    @GetMapping("/selectArg")
    public ResultBody selectArg(HttpServletRequest httpServletRequest) {
        if (!"11".equals(httpServletRequest.getParameter("id"))) {
            return ResultBody.success();
        }
        EquipmentResponseVo equipmentResponseVo = new EquipmentResponseVo();
        List<NodeVo> nodes = new ArrayList<>();

        NodeVo nodeVo1 = new NodeVo();
        nodeVo1.setId("11");
        nodeVo1.setLabel("设备");
        PropertieVo propertieVo1 = new PropertieVo();
        propertieVo1.setId("1");
        propertieVo1.setName("设备01");
        nodeVo1.setProperties(propertieVo1);
        nodes.add(nodeVo1);

        NodeVo nodeVo22 = new NodeVo();
        nodeVo22.setId("10086");
        nodeVo22.setLabel("参数");
        PropertieVo propertieVo10086 = new PropertieVo();
        propertieVo10086.setId("10086");
        propertieVo10086.setName("验收测试");
        propertieVo10086.setCsdh("TX55");
        nodeVo22.setProperties(propertieVo10086);
        nodes.add(nodeVo22);

        NodeVo nodeVo2 = new NodeVo();
        nodeVo2.setId("21");
        nodeVo2.setLabel("参数");
        PropertieVo propertieVo2 = new PropertieVo();
        propertieVo2.setId("2");
        propertieVo2.setName("地球敏感器B主份温度");
        propertieVo2.setCsdh("ZK38");
        nodeVo2.setProperties(propertieVo2);
        nodes.add(nodeVo2);


        NodeVo nodeVo3 = new NodeVo();
        nodeVo3.setId("22");
        nodeVo3.setLabel("参数");
        PropertieVo propertieVo3 = new PropertieVo();
        propertieVo3.setId("3");
        propertieVo3.setName("太阳敏感器线路盒温度");
        propertieVo3.setCsdh("ZK312");
        nodeVo3.setProperties(propertieVo3);
        nodes.add(nodeVo3);

        NodeVo PJA3E = new NodeVo();
        PJA3E.setId("101");
        PJA3E.setLabel("参数");
        PropertieVo propertieVo4 = new PropertieVo();
        propertieVo4.setId("4");
        propertieVo4.setName("星敏感器线路盒3温度");
        propertieVo4.setCsdh("ZK316");
        PJA3E.setProperties(propertieVo4);
        nodes.add(PJA3E);

        NodeVo PJA2F = new NodeVo();
        PJA2F.setId("102");
        PJA2F.setLabel("参数");
        PropertieVo propertieVo5 = new PropertieVo();
        propertieVo5.setId("5");
        propertieVo5.setName("S轴陀螺漂移补偿");
        propertieVo5.setCsdh("ZK404");
        PJA2F.setProperties(propertieVo5);
        nodes.add(PJA2F);

        NodeVo PJA3F = new NodeVo();
        PJA3F.setId("103");
        PJA3F.setLabel("参数");
        PropertieVo propertieVo6 = new PropertieVo();
        propertieVo6.setId("6");
        propertieVo6.setName("速率积分陀螺主份温度");
        propertieVo6.setCsdh("ZK528");
        PJA3F.setProperties(propertieVo6);
        nodes.add(PJA3F);

        NodeVo IKA5 = new NodeVo();
        IKA5.setId("104");
        IKA5.setLabel("参数");
        PropertieVo propertieVo7 = new PropertieVo();
        propertieVo7.setId("7");
        propertieVo7.setName("南电池B主加热器状态");
        propertieVo7.setCsdh("ZK530");
        IKA5.setProperties(propertieVo7);
        nodes.add(IKA5);

        NodeVo PJA4 = new NodeVo();
        PJA4.setId("105");
        PJA4.setLabel("参数");
        PropertieVo propertieVo8 = new PropertieVo();
        propertieVo8.setId("8");
        propertieVo8.setName("10N推力器7A备加热器继电器开关状态");
        propertieVo8.setCsdh("ZK529");
        PJA4.setProperties(propertieVo8);
        nodes.add(PJA4);

        NodeVo PJA14 = new NodeVo();
        PJA14.setId("123");
        PJA14.setLabel("参数");
        PropertieVo propertieVo14 = new PropertieVo();
        propertieVo14.setId("13");
        propertieVo14.setName("单参数据");
        propertieVo14.setCsdh("TK12");
        PJA14.setProperties(propertieVo14);
        nodes.add(PJA14);

        NodeVo PJA5 = new NodeVo();
        PJA5.setId("110");
        PJA5.setLabel("参数");
        PropertieVo propertieVo9 = new PropertieVo();
        propertieVo9.setId("9");
        propertieVo9.setName("中断数据");
        propertieVo9.setCsdh("ZK110");
        PJA5.setProperties(propertieVo9);
        nodes.add(PJA5);

        for (int i = 3; i < 11; i++) {
            NodeVo PJA = new NodeVo();
            PJA.setId(String.valueOf(i));
            PJA.setLabel("参数");
            PropertieVo propertieVo = new PropertieVo();
            propertieVo.setId(String.valueOf(i) + "110");
            propertieVo.setName("(斜率演示用)阳压" + i);
            propertieVo.setCsdh("ZK" + i);
            PJA.setProperties(propertieVo);
            nodes.add(PJA);
        }

        NodeVo PJA6 = new NodeVo();
        PJA6.setId("110");
        PJA6.setLabel("参数");
        PropertieVo propertieVo10 = new PropertieVo();
        propertieVo10.setId("10");
        propertieVo10.setName("(双参演示用)北分流器壳温TN9");
        propertieVo10.setCsdh("TN9");
        PJA6.setProperties(propertieVo10);
        nodes.add(PJA6);

        NodeVo PJA7 = new NodeVo();
        PJA7.setId("110");
        PJA7.setLabel("参数");
        PropertieVo propertieVo11 = new PropertieVo();
        propertieVo11.setId("11");
        propertieVo11.setName("(双参演示用)南分流器壳温TN10");
        propertieVo11.setCsdh("TN10");
        PJA7.setProperties(propertieVo11);
        nodes.add(PJA7);

        NodeVo PJA8 = new NodeVo();
        PJA8.setId("110");
        PJA8.setLabel("参数");
        PropertieVo propertieVo12 = new PropertieVo();
        propertieVo12.setId("12");
        propertieVo12.setName("(门限演示用)动量轮1轴承温度");
        propertieVo12.setCsdh("TK7");
        PJA8.setProperties(propertieVo12);
        nodes.add(PJA8);

        NodeVo PJA9 = new NodeVo();
        PJA9.setId("110");
        PJA9.setLabel("参数");
        PropertieVo propertieVo13 = new PropertieVo();
        propertieVo13.setId("13");
        propertieVo13.setName("(门限演示用)TK8动量轮2轴承温度");
        propertieVo13.setCsdh("TK8");
        PJA9.setProperties(propertieVo13);
        nodes.add(PJA9);



        equipmentResponseVo.setNodes(nodes);

        return ResultBody.success(equipmentResponseVo);
    }


    /**
     * 模拟接口故障数据
     * @param httpServletRequest request请求
     * @return CheckResult<Object>
     */
    @ApiOperation(value = "模拟接口故障数据")
    @PostMapping("/GeneratingFailureData")
    public CheckResult<Object> GeneratingFailureData(HttpServletRequest httpServletRequest) {
        EquipmentResponseVo equipmentResponseVo = new EquipmentResponseVo();
        ArrayList<NodeVo> nodeVoList = new ArrayList<>();
        NodeVo nodeVo = new NodeVo();
        nodeVo.setId("1");
        nodeVo.setIcon("图片1");
        nodeVo.setLabel("星");
        nodeVo.setUserId("1");
        nodeVo.setMergeFlag("mergeFlag");
        nodeVo.setShowField("showField");
        PropertieVo propertieVo = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        nodeVo.setProperties(propertieVo);
        nodeVoList.add(nodeVo);

        NodeVo nodeVo1 = new NodeVo();
        nodeVo1.setId("1");
        nodeVo1.setIcon("图片1");
        nodeVo1.setLabel("分系统");
        nodeVo1.setUserId("1");
        nodeVo1.setMergeFlag("mergeFlag");
        nodeVo1.setShowField("showField");
        PropertieVo propertieVo1 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        nodeVo1.setProperties(propertieVo1);
        nodeVoList.add(nodeVo1);

        NodeVo nodeVo2 = new NodeVo();
        nodeVo2.setId("1");
        nodeVo2.setIcon("图片1");
        nodeVo2.setLabel("设备");
        nodeVo2.setUserId("1");
        nodeVo2.setMergeFlag("mergeFlag");
        nodeVo2.setShowField("showField");
        PropertieVo propertieVo2 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        nodeVo.setProperties(propertieVo2);
        nodeVoList.add(nodeVo2);

        NodeVo nodeVo3 = new NodeVo();
        nodeVo3.setId("1");
        nodeVo3.setIcon("图片1");
        nodeVo3.setLabel("参数");
        nodeVo3.setUserId("1");
        nodeVo3.setMergeFlag("mergeFlag");
        nodeVo3.setShowField("showField");
        PropertieVo propertieVo3 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        nodeVo3.setProperties(propertieVo3);
        nodeVoList.add(nodeVo3);

        NodeVo nodeVo4 = new NodeVo();
        nodeVo4.setId("1");
        nodeVo4.setIcon("图片1");
        nodeVo4.setLabel("日期");
        nodeVo4.setUserId("1");
        nodeVo4.setMergeFlag("mergeFlag");
        nodeVo4.setShowField("showField");
        PropertieVo propertieVo4 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        nodeVo4.setProperties(propertieVo4);
        nodeVoList.add(nodeVo4);
        equipmentResponseVo.setNodes(nodeVoList);

        ArrayList<Object> pathList = new ArrayList<>();
        equipmentResponseVo.setPaths(pathList);
        ArrayList<RelationshipVo> relationshipVoArrayList = new ArrayList<>();
        RelationshipVo relationshipVo = new RelationshipVo();
        relationshipVo.setId("111");
        relationshipVo.setLabel("故障现象_参数");
        relationshipVo.setStart("start111");
        relationshipVo.setEnd("end111");
        relationshipVo.setInfer("infer111");
        PropertieVo relationshipPropertieVo = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        relationshipVo.setProperties(relationshipPropertieVo);

        RelationshipVo relationshipVo1 = new RelationshipVo();
        relationshipVo1.setId("111");
        relationshipVo1.setLabel("故障现象_日期");
        relationshipVo1.setStart("start111");
        relationshipVo1.setEnd("end111");
        relationshipVo1.setInfer("infer111");
        PropertieVo relationshipPropertieVo1 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        relationshipVo1.setProperties(relationshipPropertieVo1);

        RelationshipVo relationshipVo2 = new RelationshipVo();
        relationshipVo2.setId("111");
        relationshipVo2.setLabel("设备_参数");
        relationshipVo2.setStart("start111");
        relationshipVo2.setEnd("end111");
        relationshipVo2.setInfer("infer111");
        PropertieVo relationshipPropertieVo2 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        relationshipVo2.setProperties(relationshipPropertieVo2);

        RelationshipVo relationshipVo3 = new RelationshipVo();
        relationshipVo3.setId("111");
        relationshipVo3.setLabel("分系统_设备");
        relationshipVo3.setStart("start111");
        relationshipVo3.setEnd("end111");
        relationshipVo3.setInfer("infer111");
        PropertieVo relationshipPropertieVo3 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        relationshipVo3.setProperties(relationshipPropertieVo3);

        RelationshipVo relationshipVo4 = new RelationshipVo();
        relationshipVo4.setId("111");
        relationshipVo4.setLabel("星_分系统");
        relationshipVo4.setStart("start111");
        relationshipVo4.setEnd("end111");
        relationshipVo4.setInfer("infer111");
        PropertieVo relationshipPropertieVo4 = PropertieVo.builder().id("11").csdh("csdh").code("code").name("模拟1").build();
        relationshipVo4.setProperties(relationshipPropertieVo4);
        relationshipVoArrayList.add(relationshipVo);
        equipmentResponseVo.setRelationships(relationshipVoArrayList);
        return CheckResult.success(equipmentResponseVo);
    }


    /**
     * 模拟权限接口
     * @param o 测试入参
     * @return ResultBody
     */
    @ApiOperation(value = "权限测试")
    @GetMapping("/getLogonInfo")
    public JSONObject getLogonInfo(@RequestParam("token") String token) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("can_modify", "1");
        return jsonObject;
    }

    /**
     * 模拟权限接口token校验
     * @param token 测试入参
     * @return ResultBody
     */
    @ApiOperation(value = "权限测试token")
    @GetMapping("/checkToken")
    public JSONObject checkToken(@RequestParam("token") String token) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("postLevel", "1");
        return jsonObject;
    }
}
